package com.ygqh.baby.controller.admin;
import java.util.*;
import java.util.stream.Collectors;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.model.ProductBookRecordModel;
import com.ygqh.baby.service.*;
import com.ygqh.baby.shiro.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.po.YgProductBook;
import com.ygqh.baby.po.YgProductBookDetail;
import com.ygqh.baby.utils.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/admin/productbook")
public class ProductBookController extends BaseController<Object, java.lang.Long> {
	@Autowired
	private YgProductBookService ygProductBookService;
	@Autowired
	private YgProductBookDetailService ygProductBookDetailService;
	@Autowired
	private YgPrepayBookRecordService ygPrepayBookRecordService;
	@Autowired
	private FileService fileService;
	@Autowired
	private YgReturnLogService ygReturnLogService;

	@RequestMapping("searchProductBook")
	@ResponseBody
	public Map<String, Object> searchProductBook(String q, QueryInfo queryInfo, Date startDate, Date endDate, PromotionStatus pStatus) {

		ResultSet<YgProductBook> resultSet = ygProductBookService.search(queryInfo, getSession().getSupplierId(), pStatus, startDate, endDate, q);

		return resultSet.toDatatables(null);
	}

	@RequestMapping("addProductBook")
	@ResponseBody
	public Message addProductBook(String book) {
		YgProductBook productBook = JsonUtils.toObject(book, YgProductBook.class);	
		productBook.setCreateBy(getSession().getTrueName());
		productBook.setCreateTime(new Date());
		List<Long> productIds=new ArrayList<Long>(productBook.getProductBookDetails().size());
		List<YgProductBookDetail> pp=new ArrayList<YgProductBookDetail>(productBook.getProductBookDetails().size());	
		
		for(YgProductBookDetail p:productBook.getProductBookDetails()){
			if(!productIds.contains(p.getProductId())){
				productIds.add(p.getProductId());
				pp.add(p);
			}
		}	
		productBook.setProductBookDetails(pp);
		return ygProductBookService.save(productBook) == 1 ? SUCCESS_MESSAGE : ERROR_MESSAGE;

	}

	@RequestMapping("updateProductBook")
	@ResponseBody
	public Message updateProductBook(String book) {
		YgProductBook productBook = JsonUtils.toObject(book, YgProductBook.class);
		productBook.setUpdateBy(getSession().getTrueName());
		productBook.setUpdateTime(new Date());
		List<Long> productIds=new ArrayList<Long>(productBook.getProductBookDetails().size());
		List<YgProductBookDetail> pp=new ArrayList<YgProductBookDetail>(productBook.getProductBookDetails().size());	
		
		for(YgProductBookDetail p:productBook.getProductBookDetails()){
			if(!productIds.contains(p.getProductId())){
				productIds.add(p.getProductId());
				pp.add(p);
			}
		}	
		productBook.setProductBookDetails(pp);
		return ygProductBookService.update(productBook) == 1 ? SUCCESS_MESSAGE : ERROR_MESSAGE;

	}

	@RequestMapping("getProductBook")
	@ResponseBody
	public YgProductBook getProductBook(Long id) {
		return ygProductBookService.findById(id);
	}
	@RequestMapping("getProductListByProductBookId")
	@ResponseBody
	public  Map<String, Object> getProductListByProductBookId(Long bookId) {
	   List<Map<String,Object>> list=	ygProductBookDetailService.search(bookId);
		ResultSet<Map<String,Object>> resultSet=new ResultSet<Map<String,Object>>(1,list);
		
		return resultSet.toDatatables(null);
	}
	
	@RequestMapping("getBookTotal")
	@ResponseBody
	public Map<String,Object> getBookTotal(Long id){
		List<Map<String,Object>> list=	ygPrepayBookRecordService.search(id);
		ResultSet<Map<String,Object>> resultSet=new ResultSet<Map<String,Object>>(1,list);
		return resultSet.toDatatables(null);
	}

	@RequestMapping("downloadProductDetail")
	@ResponseBody
	public void downloadProductDetail(Long id, HttpServletRequest request, HttpServletResponse response) {
		List<Map<String,Object>> list=	ygPrepayBookRecordService.search(id);
		byte[] bytes = fileService.downloadFromOss(FileTemplate.ProductBookProductExport.getCode());
		Map<String, Object> dataMap = new HashMap<String, Object>(1);
		dataMap.put("resultSet", list);
		DocumentHandler.DownloadExcel(bytes, FileTemplate.ProductBookProductExport.getTitle(), dataMap, request, response);
	}

	@RequestMapping("searchRecordList")
	@ResponseBody
	public Map<String, Object> searchRecordList(QueryInfo queryInfo, String userName, String recordCode) {
		ResultSet<ProductBookRecordModel> resultSet = ygPrepayBookRecordService.search(queryInfo, recordCode, null, null, userName);
		return resultSet.toDatatables(null);
	}

	@RequestMapping("refundRecord")
	@ResponseBody
	public Message refundRecord(@RequestParam Long recordCode) {
		Message message = ygPrepayBookRecordService.refundRecord(recordCode);
		ygReturnLogService.addReturnLog(recordCode.toString(), recordCode, "定金退款", SessionUtil.getCurrentUser().getTrueName(), message.getContent());
		return message;
	}

}
